package com.youlai.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.Role;
import com.youlai.system.model.form.MyRoleForm;
import com.youlai.system.model.query.RoleQuery;
import com.youlai.system.model.vo.RoleVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "09.我的角色接口")
@RestController
@RequestMapping("/api/v1/myrole")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    public PageResult<RoleVO> getRolePage(
            @ParameterObject RoleQuery queryParams
    ) {
        Page<RoleVO> result = roleService.getRoelPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增或修改角色")
    @PostMapping
    @PreventDuplicateSubmit
    public Result saveDept(
            @Valid @RequestBody MyRoleForm formData
    ) {
        Integer id = roleService.saveRole(formData);
        return Result.success(id);
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{ids}")
    public Result deleteDepartments(
            @Parameter(description ="角色ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = roleService.deleteByIds(ids);
        return Result.judge(result);
    }

    @Operation(summary = "角色表单数据")
    @GetMapping("/{id}/form")
    public Result<MyRoleForm> getRoleForm(
            @Parameter(description = "角色id") @PathVariable Long id
    ) {
        MyRoleForm roleForm = roleService.getRoleForm(id);
        return Result.success(roleForm);
    }

    @Operation(summary = "已启用的角色列表")
    @GetMapping("/getRoleList")
    public Result getRoleList(){
        List<Role> list =  roleService.getLIstRole();
        return Result.success(list);
    }


    @Operation(summary = "分配权限")
    @PostMapping("/assignPrem")

    public Result assignPrem( Integer id, String permIds){
        Integer ids =  roleService.insertPermIds(id,permIds);
        return Result.success(ids);
    }

}
